//
//  SAExpirationPaymentView.m
//  saasRetail
//
//  Created by mac on 2023/6/24.
//

#import "SAExpirationPaymentView.h"
#import "UILabel+NSMutableAttributedString.h"
#import "SAUserExpireModel.h"
#import "SALoginVC.h"

@interface SAExpirationPaymentView ()

@property (nonatomic, strong) SAUserExpireModel *expireModel;

@end

@implementation SAExpirationPaymentView

-(instancetype)initWithFrame:(CGRect)frame AndModel:(SAUserExpireModel *)model {
    if (self = [super initWithFrame:frame]) {
        self.expireModel = model;
        [self initUI];
    }
    return self;
}

#pragma mark - event
-(void)clickedBgBtn {
//    if ([self.expireModel.termType isEqualToString:@"3"]) {
//        [SAValueManger shareInStance].userInfo = nil;
//        [kUserDefault setObject:@"" forKey:kAotuLoginKey];
//        UIWindow *window =  [UIApplication sharedApplication].keyWindow;
//        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[SALoginVC new]];
//        window.rootViewController = nav;
//        [window makeKeyAndVisible];
//        return;
//    }
    [self removeFromSuperview];
}

-(void)clickedPayBtn {
    !self.payBlock ?: self.payBlock();
}

#pragma mark - initUI
-(void)initUI {
    
    UIButton *bgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    bgBtn.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    [bgBtn addTarget:self action:@selector(clickedBgBtn) forControlEvents:UIControlEventTouchUpInside];
    bgBtn.backgroundColor = kColor(0, 0, 0, 0.6);
    [self addSubview:bgBtn];
    
    UIView *bgView = [UIView new];
    bgView.frame = CGRectMake(kScaleNum(30), (kScreenHeight - kScaleNum(440))/2, kScreenWidth - kScaleNum(60), kScaleNum(440));
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.cornerRadius = 18;
    bgView.layer.masksToBounds = YES;
    [self addSubview:bgView];
    
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth - kScaleNum(60), kScaleNum(167))];
    imageV.image = [UIImage imageNamed:@"SAExpiration_top"];
    [bgView addSubview:imageV];
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    closeBtn.frame = CGRectMake(kScreenWidth - kScaleNum(100), 0, kScaleNum(40), kScaleNum(40));
    [closeBtn addTarget:self action:@selector(clickedBgBtn) forControlEvents:UIControlEventTouchUpInside];
    [closeBtn setTitle:@"X" forState:UIControlStateNormal];
    [closeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    closeBtn.titleLabel.font = kFont(20);
    [bgView addSubview:closeBtn];
    
    UILabel *trialLab = [[UILabel alloc] initWithFrame:CGRectMake(0, kScaleNum(200), kScreenWidth - kScaleNum(60), kScaleNum(20))];
    trialLab.text = @"您的云存储使用期限还剩";
    trialLab.font = kFont(18);
    trialLab.textColor = kBlackColor;
    trialLab.textAlignment = NSTextAlignmentCenter;
    [bgView addSubview:trialLab];
    
    NSString *date = [NSString stringWithFormat:@"%zd天",[self getDifferenceByDate:self.expireModel.dueDate]];
    if ([self.expireModel.termType isEqualToString:@"3"]) {
        date = @"0天";
    }
    UILabel *dateLab = [[UILabel alloc] initWithFrame:CGRectMake(0, kScaleNum(240), kScreenWidth - kScaleNum(60), kScaleNum(70))];
    dateLab.text = date;
    dateLab.font = kBFont(60);
    dateLab.textColor = [UIColor orangeColor];
    dateLab.textAlignment = NSTextAlignmentCenter;
    [bgView addSubview:dateLab];
    [dateLab setAttributedStringWithSubString:@"天" color:kBlackColor font:kFont(15)];
    
    UILabel *tipLab = [[UILabel alloc] initWithFrame:CGRectMake(0, kScaleNum(330), kScreenWidth - kScaleNum(60), kScaleNum(20))];
    tipLab.text = @"到期请及时续费";
    tipLab.font = kFont(18);
    tipLab.textColor = kBlackColor;
    tipLab.textAlignment = NSTextAlignmentCenter;
    [bgView addSubview:tipLab];
    
    UIButton *payBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    payBtn.frame = CGRectMake((kScreenWidth - kScaleNum(261))/2, kScaleNum(370), kScaleNum(201), kScaleNum(44));
    [payBtn addTarget:self action:@selector(clickedPayBtn) forControlEvents:UIControlEventTouchUpInside];
    [payBtn setBackgroundImage:[UIImage imageNamed:@"SAExpiration_pay"] forState:UIControlStateNormal];
    [bgView addSubview:payBtn];
}

- (NSInteger)getDifferenceByDate:(NSString *)date {
    //获得当前时间
    NSDate *now = [NSDate date];
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式
    [dateFormatter setDateFormat:@"yyyy/MM/dd"];
    
    NSString *nowDateStr = [dateFormatter stringFromDate:now];
    NSDate *nowDate = [dateFormatter dateFromString:nowDateStr];
    
    NSDate *oldDate = [dateFormatter dateFromString:date];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    unsigned int unitFlags =NSCalendarUnitDay;
    NSDateComponents *comps = [gregorian components:unitFlags fromDate:nowDate toDate:oldDate options:0];
    return [comps day];
}

@end
